package com.ym.b2b.news.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.ym.b2b.commons.dao.BaseDao;
import com.ym.b2b.news.dao.EmBNewsDAO;
import com.ym.b2b.news.sqlbean.EmBNews;
import com.zc.commons.base.Page;

@Repository
public class EmBNewsDAOImpl<T> extends BaseDao<T> implements EmBNewsDAO {

    @Override
    public Long searchPageEmBNewsNum(EmBNews embnews) throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", embnews);
        return (Long) listNum("EM_B_NEWS.searchPageEmBNewsNum", map);
    }

    @SuppressWarnings("unchecked")
    @Override
    public Page<EmBNews> searchPageEmBNews(EmBNews embnews, Page<EmBNews> page) throws DataAccessException {
        page.setTotalRow(searchPageEmBNewsNum(embnews));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", embnews);
        map.put("startrow", page.getStartRow());
        map.put("pagesize", page.getPageSize());
        map.put("pageable", page.getPageable());
        page.setRows(list("EM_B_NEWS.searchPageEmBNews", map));
        return page;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Page<EmBNews> searchMobilePageEmBNews(EmBNews embnews, Page<EmBNews> page) throws DataAccessException {
        page.setTotalRow(searchPageEmBNewsNum(embnews));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", embnews);
        map.put("startrow", page.getStartRow());
        map.put("pagesize", page.getPageSize());
        map.put("pageable", page.getPageable());
        page.setRows(list("EM_B_NEWS.searchMobilePageEmBNews", map));
        return page;
    }

    @Override
    public void insertEmBNews(EmBNews record) throws DataAccessException {
        insert("EM_B_NEWS.insertEmBNews", record);
    }

    @Override
    public int updateEmBNews(EmBNews record) throws DataAccessException {
        int rows = update("EM_B_NEWS.updateEmBNews", record);
        return rows;
    }

    @Override
    public EmBNews searchEmBNews(Long newsId) throws DataAccessException {
        EmBNews emBNews = new EmBNews();
        emBNews.setNewsId(newsId);
        emBNews = (EmBNews) find("EM_B_NEWS.searchEmBNews", emBNews);
        return emBNews;
    }

    @Override
    public int deleteEmBNews(Long newsId) throws DataAccessException {
        EmBNews key = new EmBNews();
        key.setNewsId(newsId);
        int rows = delete("EM_B_NEWS.deleteEmBNews", key);
        return rows;
    }

    @Override
    public int deleteMultipleEmBNews(String newsIdList) {
        int rows = delete("EM_B_NEWS.deleteMultipleEmBNews", newsIdList);
        return rows;
    }

    @Override
    public void updateEmBNewsBat(EmBNews record) throws DataAccessException {
        EmBNews emBNews = new EmBNews();
        emBNews.setNewsStatus(record.getNewsStatus());
        emBNews.setIsIndex(record.getIsIndex());
        emBNews.setIsChannel(record.getIsChannel());
        emBNews.setIsCommend(record.getIsCommend());
        emBNews.setIsScroll(record.getIsScroll());
        emBNews.setIsTop(record.getIsTop());

        emBNews.setNewsIDList(record.getNewsIDList());
        update("EM_B_NEWS.updateEmBNewsBat", emBNews);
    }

    @Override
    public List<EmBNews> searchSimpleNewsList(EmBNews emBNews) throws DataAccessException {
        return list("EM_B_NEWS.searchSimpleNewsList", emBNews);
    }

}
